package com.yc.clouduser.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yc.clouduser.entity.Chat;
import com.yc.clouduser.mapper.ChatMapper;
import com.yc.clouduser.service.IChatService;
import com.yc.vcode.bean.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzk
 * @since 2024-10-09
 */
@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    ChatMapper chatMapper;

    //存储消息
    @RequestMapping("insayData")
    Result insayData(int cid, int uid, String say){
        try{
            Chat chat = new Chat();
            chat.setCid(cid);
            chat.setUid(uid);
            chat.setSay(say);
            chat.setStatus(1);
            chatMapper.insert(chat);
            return new Result(1, "添加成功", null);
        }catch (Exception e){
            return new Result(-1, "系统超时", null);
        }
    }

    //查询消息
    @RequestMapping("getSayData")
    Result getSayData(String cid){
        try{
            LambdaQueryWrapper<Chat> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Chat::getCid, cid);
            lqw.orderByAsc(Chat::getTime);
            List<Chat> chats = chatMapper.selectList(lqw);
            return new Result(1, "消息", chats);
        }catch (Exception e){
            return new Result(-1, "系统超时", null);
        }
    }

    @Autowired
    IChatService iChatService;

    @RequestMapping("one")
    Result one(int uid, int cid){
        return iChatService.one(uid, cid);
    }

    @RequestMapping("all")
    Result all(int cid){
        return iChatService.all(cid);
    }

}

